package com.glory.hikvisionmobile.adapter;

import android.util.SparseBooleanArray;
import android.widget.CheckBox;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.bean.LotSplitOrAdd.PdaComponentBean;

import java.util.List;

public class LotSplitListAdapter extends BaseQuickAdapter<PdaComponentBean, BaseViewHolder> {

    private SparseBooleanArray mCheckStates = new SparseBooleanArray();
    public LotSplitListAdapter(@Nullable List<PdaComponentBean> data) {
        super(R.layout.adapter_param_list,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, PdaComponentBean item) {
        helper.setText(R.id.tvItem1,item.getComponentId());
        helper.setText(R.id.tvItem2,item.getCompSeq());
        helper.setText(R.id.tvItem3,item.getState());
        helper.setText(R.id.tvItem4,item.getCompQty());
        helper.setGone(R.id.tvItem5,false);
        helper.setGone(R.id.cbLayout,true);
        CheckBox checkBox = helper.getView(R.id.pauseCb);
        checkBox.setEnabled(true);
        checkBox.setTag(helper.getAdapterPosition());
        checkBox.setChecked(mCheckStates.get(helper.getAdapterPosition(), false));
        checkBox.setOnClickListener(v -> {
            if (checkBox.isChecked()) {
                mCheckStates.put(helper.getAdapterPosition(), true);
            } else {
                mCheckStates.put(helper.getAdapterPosition(), false);
            }

            notifyItemChanged(helper.getAdapterPosition());
        });
    }

    public SparseBooleanArray getDataCheck(){
        return mCheckStates;
    }

    public void clearPos() {
        mCheckStates = new SparseBooleanArray();
    }

    public String getCommentID(){
        StringBuffer sb = new StringBuffer();
        List<PdaComponentBean> data = getData();
        for (int i = 0; i < data.size(); i++) {
            if (mCheckStates.get(i)){
                sb.append(data.get(i).getComponentId() + ";");
            }
        }

        return sb.toString();
    }
}
